	/**
 * Created by Richard Radlherr.
 * Date: 06.06.2006 19:04
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using TagTool.Core;

namespace TagTool.Components
{
	
	public class NodeDoubleClickedEventArgs
	{
		BaseNode node;

		public NodeDoubleClickedEventArgs(BaseNode node)
		{
			this.node = node;
		}

		public BaseNode Node
		{
			get { return node; }
		}
	}
	
	/**
	 * Delegate which is called to inform the main window, that a new node shal be created.
	 */
	public delegate void NodeDoubleClickedEventHandler(Object sender, NodeDoubleClickedEventArgs e);
	
	public partial class NodeBrowsePanel
	{
		
		public event NodeDoubleClickedEventHandler NodeDoubleClick;

		public NodeBrowsePanel()
		{
			InitializeComponent();
			nodes.ContextMenuStrip = contextMenu;
		}
		
		public void LoadAssembly()
		{
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				if (!AssemblyManager.ContainsAssembly(openFileDialog.FileName))
				{
					int i = openFileDialog.FileName.LastIndexOf('\\');
					String s = openFileDialog.FileName.Substring(i + 1, openFileDialog.FileName.Length - i - 1);
					AssemblyManager.GetInstance(s);
					AssemblyManager.TraverseAssembliy(s, OnAssemblyTraverse);
				}
			}
		}
		
		private void NodesNodeMouseDoubleClick(object sender, System.Windows.Forms.TreeNodeMouseClickEventArgs e)
		{
			if (NodeDoubleClick != null && e.Node.Parent != null)
			{
				NodeDoubleClick(this, new NodeDoubleClickedEventArgs(ProjectManager.Instance.CreateNode(e.Node.Parent.Text, e.Node.Text)));
			}
		}
		
		private void NodesItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
		{
			if (e.Item is TreeNode)
			{
				if (((TreeNode)e.Item).Parent != null)
				{
					nodes.DoDragDrop(e.Item, DragDropEffects.Move);
				}
			}
			
		}
		
		private void NodesNodeMouseClick(object sender, System.Windows.Forms.TreeNodeMouseClickEventArgs e)
		{
			nodes.SelectedNode = e.Node;
		}
		
		private void RemoveAssemblyToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			if (nodes.SelectedNode != null && nodes.SelectedNode.Parent == null)
			{
				nodes.Nodes.Remove(nodes.SelectedNode);
			}
		}
		
		private void ContextMenuOpening(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (nodes.SelectedNode == null)
			{
				removeAssembly.Enabled = false;
			}
			else
			{
				if (!(nodes.SelectedNode.Tag is Assembly))
				{
					removeAssembly.Enabled = false;
				}
				else
				{
					removeAssembly.Enabled = true;
				}
			}
		}
		
		private void AddAssemblyClick(object sender, System.EventArgs e)
		{
			LoadAssembly();
		}
		
		private void OnAssemblyTraverse(String assembly, Type nodeType, bool newAssembly)
		{
			if (newAssembly)
			{
				if (!nodes.Nodes.ContainsKey(assembly))
				{
					nodes.Nodes.Add(assembly,assembly);
				}
			}
			else
			{
				if(!nodes.Nodes[assembly].Nodes.ContainsKey(nodeType.FullName))
				{
                    nodes.Nodes[assembly].Nodes.Add(nodeType.FullName, nodeType.FullName);
				}
			}
		}
		
		public void InitializeTreeView()
		{
			AssemblyManager.TraverseAssemblies(OnAssemblyTraverse);
		}
		
	}
}
